package com.djl.zenithbite.service.user.impl;

import com.djl.zenithbite.common.response.BaseResponse;
import com.djl.zenithbite.common.threadlocal.UserContextHolder;
import com.djl.zenithbite.entity.*;
import com.djl.zenithbite.repository.*;
import com.djl.zenithbite.service.user.UserProductQueryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class UserProductQueryServiceImpl implements UserProductQueryService {

    @Autowired
    private StoreMapper storeMapper;

    @Autowired
    private ProductMapper productMapper;

    @Autowired
    private OrderMapper orderMapper;

    @Autowired
    private OrderItemMapper orderItemMapper;

    @Autowired
    private TagRelationMapper tagRelationMapper;

    @Override
    public List<Product> storeList() {

        User user = UserContextHolder.getUser();

        //get user order
        List<Order> userOrder = getUserOrder(user);

        //get product
        if (!CollectionUtils.isEmpty(userOrder)){
            List<Product> product = getProduct(userOrder);
            if (!CollectionUtils.isEmpty(product)){
                TagRelationExample example = new TagRelationExample();
                TagRelationExample.Criteria criteria = example.createCriteria();
                criteria.andEntityIdIn(product.stream().map(Product::getProductId).distinct().collect(Collectors.toList()));
                List<TagRelation> tagRelations = tagRelationMapper.selectByExample(example);
                return productMapper.queryByProductId(tagRelations.stream().map(TagRelation::getEntityId).distinct().collect(Collectors.toList()));

            }
        }else {
            return productMapper.selectByExample(new ProductExample());
        }

        return new ArrayList<>();
    }

    private List<Product> getProduct(List<Order> userCart) {

        List<Product> list = new ArrayList<>();
        for (Order order : userCart){
            OrderItemExample orderItemExample = new OrderItemExample();
            OrderItemExample.Criteria orderItemExampleCriteria = orderItemExample.createCriteria();
            orderItemExampleCriteria.andOrderIdEqualTo(order.getOrderId());
            List<OrderItem> orderItems = orderItemMapper.selectByExample(orderItemExample);

            ProductExample example = new ProductExample();
            ProductExample.Criteria criteria = example.createCriteria();
            criteria.andProductIdIn(orderItems.stream().map(OrderItem::getProductId).collect(Collectors.toList()));
            list.addAll(productMapper.selectByExample(example));
        }
        return list;
    }

    private List<Order> getUserOrder(User user) {
        OrderExample example = new OrderExample();
        OrderExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(user.getId());
      return orderMapper.selectByExample(example);
    }
}
